package eg.backend.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import eg.backend.base.GoalStatus;
import eg.backend.domain.Goal;
import eg.backend.manager.GoalManager;
import eg.backend.manager.UserManager;
import eg.backend.repository.GoalRepository;

public class GoalManagerImpl implements GoalManager {
	@Autowired
	private UserManager userManager;
	@Autowired
	private GoalRepository goalRepository;
	
	public Goal update(Goal goal) {
		if(goal.getEncodedKey() == null) {
			goal.setCreatedTime(new Date());
			goal.setOwner(userManager.getCurrentUserId());
			goal.setStatus(GoalStatus.CREATED);
		}
		return goalRepository.save(goal);
	}
	
	public List<Goal> findUserGoals() {
		return goalRepository.findByUser(userManager.getCurrentUserId());
	}

	public List<Goal> findUserGoalsByStatus(GoalStatus status) {
		return goalRepository.findByUserAndStatus(status, userManager.getCurrentUserId());
	}

}
